#include "Cursor.h"
#include "boost\filesystem.hpp"
#include <string>
#include <sstream>
#include "Logger.h"
#include "Game.h"

Cursor::Cursor() {
	this->pTexture = NULL;
}

Cursor::~Cursor() {
	this->pTexture->Release();
}

int Cursor::Initialize() {
	SecureZeroMemory(&this->Quad, sizeof(this->Quad));


	string texFile = "Textures/Cursor.png";
	boost::filesystem::path texFilePath = boost::filesystem::complete(texFile);

	ID3D11ShaderResourceView* pTexture = NULL;

	if(FAILED(D3DX11CreateShaderResourceViewFromFile(Game::GraphicsPtr->dev,
													 (TCHAR*)texFilePath.c_str(),
													 NULL,
													 NULL,
													 &this->pTexture,
													 NULL))) {
		ostringstream message;
		message << "Failed to load Cursor texture '" << texFile << "'! Please check if the file exists!";
		Logger::Log(E_GRAPHICS_LOADTEX_FAILED, message.str(), __FILE__, __LINE__, LOG_ERROR);
	}

	this->Quad[0].Color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	this->Quad[1].Color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	this->Quad[2].Color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	this->Quad[3].Color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

	this->Quad[0].Z = 0.0f;
	this->Quad[1].Z = 0.0f;
	this->Quad[2].Z = 0.0f;
	this->Quad[3].Z = 0.0f;

	this->Quad[0].U = 0.0f;
	this->Quad[0].V = 0.0f;
		
	this->Quad[1].U = 1.0f;
	this->Quad[1].V = 0.0f;
		
	this->Quad[2].U = 0.0f;
	this->Quad[2].V = 1.0f;
		
	this->Quad[3].U = 1.0f;
	this->Quad[3].V = 1.0f;

	this->Quad[0].X = - (16.0f / (float)windowSize.right);
	this->Quad[0].Y = (16.0f / (float)windowSize.bottom);

	this->Quad[1].X = (16.0f / (float)windowSize.right);
	this->Quad[1].Y = (16.0f / (float)windowSize.bottom);

	this->Quad[2].X = - (16.0f / (float)windowSize.right);
	this->Quad[2].Y = - (16.0f / (float)windowSize.bottom);

	this->Quad[3].X = (16.0f / (float)windowSize.right);
	this->Quad[3].Y = - (16.0f / (float)windowSize.bottom);

	return 0;
}

void Cursor::UpdateQuad(int xPos, int yPos) {

	float absoluteX = (-1.0f) + 2 * ((float)xPos / (float)windowSize.right);
	float absoluteY = (1.0f) - 2 * ((float)yPos / (float)windowSize.bottom);

	this->Quad[0].X =  absoluteX;
	this->Quad[0].Y = absoluteY;

	this->Quad[1].X =  2 * (16.0f / (float)windowSize.right) + absoluteX;
	this->Quad[1].Y = absoluteY;

	this->Quad[2].X = absoluteX;
	this->Quad[2].Y = absoluteY - (2*(16.0f / (float)windowSize.bottom));

	this->Quad[3].X = 2 * (16.0f / (float)windowSize.right) + absoluteX;
	this->Quad[3].Y = absoluteY - (2*(16.0f / (float)windowSize.bottom));

}